<?php

namespace app\modules\user\controllers;

use yii;
use yii\web\Controller;
use common\models\Tools;
use common\models\Works;
use common\models\Goods;
/**
 * 我的资源
 */
class GoodslistController extends Controller
{
    public $enableCsrfValidation=false;
	public $leftmenu;
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        //$this->layout = "/personal";
        //检测登录情况
        if (Yii::$app->user->isGuest) {
            return $this->redirect('/site/login?returnurl=/user/goodslist/index');
        }
        
        //获取商家的商品列表
        $data = \common\models\Goods::getGoodslistBySellerId(Yii::$app->user->identity->id);
        
        $data['username'] = Yii::$app->user->identity->username;
        
        // echo "<pre>";print_r($data);exit;
        $data['status_arr'] = array(0=>'待审核',1=>'在售',2=>'下架',3=>'未通过',4=>'已删除');
            
        
        $this->getView()->title = "我的资源";
        $this->layout = "/personal";
		$this->leftmenu='goodslist';
        return $this->render('index',['data'=>$data]);
    }
    
    public function actionSetStatus(){
        //检测登录情况
        if (Yii::$app->user->isGuest) {
            return $this->redirect('/site/login?returnurl=/user/goodslist/index');
        }
        
        $goods_id = Tools::GetSafeParam('goods_id', 0, 0);
        $status = Tools::GetSafeParam('status', 0, 0);
        $model = Goods::findOne($goods_id);
        if($model->seller_id != Yii::$app->user->identity->id){
            $output['success'] = 0;
            $output['msg'] = '没有权限！';
            Tools::outputjson($output);
        }
        
        $model->status = $status;
        if($model->save()){
            $output['success'] = 1;
            $output['msg'] = '操作成功！';
            Tools::outputjson($output);
        }else{
            $output['success'] = 0;
            $output['msg'] = $model->errors;
            Tools::outputjson($output);
        }
    }
    
    
}
